/**
 * Author:  LiShibin.
 * Date:    2017/7/6 0006.
 * File：   Promise的扩展，添加done和finally方法
 */
function init() {
  // 捕捉到任何可能出现的错误，并向全局抛出, 链式调用
  Promise.prototype.done = function (onFulfilled, onRejected) {
    this.then(onFulfilled, onRejected)
      .catch(function (reason) {
        // 抛出一个全局错误
        setTimeout(() => {
          throw reason
        }, 0)
      })
  }
  // 不管Promise对象最后状态如何，都会执行的操作, 链式调用
  Promise.prototype.finally = function (callback) {
    let P = this.constructor
    return this.then(
      value => P.resolve(callback()).then(() => value),
      reason => P.resolve(callback()).then(() => {
        throw reason
      })
    )
  }
}

export {init}